package cn.e4j.modular.crm.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.Gwcq001Export;
import cn.e4j.modular.crm.service.impl.Gwcq001ServiceImpl;
import cn.e4j.modular.erp.controller.ErpLogic;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.jsonwebtoken.lang.Collections;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单一览控制类
 *
 * @author mengyb
 * @Date 2019/12/10
 */
@Controller
@RequestMapping("/crm/gwcq001")
public class Gwcq001Controller extends BaseController {

    @Autowired
    private Gwcq001ServiceImpl gwcq001Service;

    @Autowired
    private SysAuthorityService sysAuthorityService;

    @Autowired
    private ErpLogic erpLogic;

    /**
     * 跳转页面
     */
    @RequiresPermissions("crm:gwcq001:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwcq001.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getList")
    @RequiresPermissions("crm:gwcq001:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        String ORG_ID = shiroUser.getORG_ID();
//        if(sysAuthorityService.getNolimitOrg(ORG_ID,null) == null ){
//            //客户权限
//            inDto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(inDto));
//            //部门权限
//            List<Dto> orgs = sysAuthorityService.getOrgsAuthority(inDto);
//            if(orgs.size() > 0) {
//                orgs.removeIf(orgDto -> orgDto.getString("ID").equals(ORG_ID));
//            }
//            inDto.put("CURR_USER",shiroUser.getID());
//            inDto.put("ORGS", orgs);
//        }
        List<Dto> result = this.gwcq001Service.getList(page, inDto);
        try {
            if (!Collections.isEmpty(result)) {
                for (Dto dtoInfo : result) {
                    inDto.put("XMDAUA001", dtoInfo.getString("GWCA005"));//合同编号
                    inDto.put("XMDCUA005", dtoInfo.getString("GWCB002"));//车型
                    inDto.put("XMDCUA002", dtoInfo.getString("GWCA004"));//配置情况
                    //获取出货单过账日期

                    //Dto xmdk001Dto = this.baseMapper.getXmdk001(dto);
                    Dto xmdk001Dto = erpLogic.getERPService("getXmdk001", inDto);
                    //获取已出货数量
                    //Dto xmdd014Dto = this.baseMapper.getXmdd014(dto);
                    Dto xmdd014Dto = erpLogic.getERPService("getXmdd014", inDto);

                    if (xmdk001Dto != null && xmdk001Dto.getString("XMDK001") != null) {
                        dtoInfo.put("XMDK001", xmdk001Dto.getString("XMDK001"));
                    }

                    if (xmdd014Dto != null && xmdd014Dto.getBigDecimal("XMDD014") != null) {
                        dtoInfo.put("XMDD014", xmdd014Dto.getBigDecimal("XMDD014"));//已发车辆
                        BigDecimal subQty = dtoInfo.getBigDecimal("GWCB006")
                                .subtract((xmdd014Dto.getBigDecimal("XMDD014")));//未发车辆
                        dtoInfo.put("XMDD014_ELSE", subQty);
                    } else {
                        dtoInfo.put("XMDD014", 0);
                        dtoInfo.put("XMDD014_ELSE", dtoInfo.get("GWCB006"));
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new PageReData(page, result);
    }

    @RequestMapping(value = "/exportsData", method = RequestMethod.GET)
    @RequiresPermissions("crm:gwcq001:init")
    public void exportsData(ModelMap map, HttpServletRequest request, HttpServletResponse response, @DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> list =  this.gwcq001Service.getListData(inDto);
        try {
            if (!Collections.isEmpty(list)) {
                for (Map<String, Object> dtoInfo : list) {
                    inDto.put("XMDAUA001", dtoInfo.get("GWCA005"));//合同编号
                    inDto.put("XMDCUA005", dtoInfo.get("GWCB002"));//车型
                    inDto.put("XMDCUA002", dtoInfo.get("GWCA004"));//配置情况
                    //获取出货单过账日期

                    //Dto xmdk001Dto = this.baseMapper.getXmdk001(dto);
                    Dto xmdk001Dto = erpLogic.getERPService("getXmdk001", inDto);
                    //获取已出货数量
                    //Dto xmdd014Dto = this.baseMapper.getXmdd014(dto);
                    Dto xmdd014Dto = erpLogic.getERPService("getXmdd014", inDto);

                    if (xmdk001Dto != null && xmdk001Dto.getString("XMDK001") != null) {
                        dtoInfo.put("XMDK001", xmdk001Dto.getString("XMDK001"));
                    }

                    if (xmdd014Dto != null && xmdd014Dto.getBigDecimal("XMDD014") != null) {
                        dtoInfo.put("XMDD014", xmdd014Dto.getBigDecimal("XMDD014"));//已发车辆
                        BigDecimal subQty = new BigDecimal(dtoInfo.get("GWCB006").toString())
                                .subtract((xmdd014Dto.getBigDecimal("XMDD014")));//未发车辆
                        dtoInfo.put("XMDD014_ELSE", subQty);
                    } else {
                        dtoInfo.put("XMDD014", 0);
                        dtoInfo.put("XMDD014_ELSE", dtoInfo.get("GWCB006"));
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        JSONArray json_arr = new JSONArray();
        for (Map<String, Object> map1 : list) {
            JSONObject json_obj = new JSONObject();
            for (Map.Entry<String, Object> entry : map1.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                json_obj.put(key, value);
            }
            json_arr.add(json_obj);
        }
        List<Gwcq001Export> gwcq001Exports = new ArrayList<>();
        gwcq001Exports = JSONObject.parseArray(json_arr.toString(), Gwcq001Export.class);
        ExportParams params = new ExportParams(null, "合同订单信息", ExcelType.XSSF);
        //params.setFreezeCol(2);
        map.put(NormalExcelConstants.FILE_NAME, "合同订单信息");
        map.put(NormalExcelConstants.DATA_LIST, gwcq001Exports);
        map.put(NormalExcelConstants.CLASS, Gwcq001Export.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }
}
